package com.sikaryofficial.file.utils;

import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 压缩工具类
 *
 * @Author pzy
 * @Description: TODO 需要多次测试调整参数
 * @Version 0.1.0
 */
public class CompressionUtils {
    public static MultipartFile getMulFileByFile(InputStream fis, String fieldName, String contentType, String fileName) {
        FileItem fileItem = createFileItem(fis, fieldName, contentType, fileName);
        return new CommonsMultipartFile(fileItem);
    }

    private static FileItem createFileItem(InputStream fis, String fieldName, String contentType, String fileName) {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem item = factory.createItem(fieldName, contentType, false, fileName);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        try {
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return item;
    }

    public static MultipartFile compressImg(MultipartFile multiFile) {
        // 压缩图片
        InputStream inputStream = null;
        ByteArrayOutputStream bos = null;
        MultipartFile multipartFile = null;
        InputStream fileInput = null;
        try {
            inputStream = multiFile.getInputStream();
            bos = new ByteArrayOutputStream();
            // 压缩图片核心代码
            Thumbnails.Builder<? extends InputStream> builder = Thumbnails.of(inputStream);
            // scale 1f 等比缩放  ; outputQuality 1高质量 -> 0 低质量
            builder.scale(1f)
                    .outputQuality(0.6f)
                    .toOutputStream(bos);
            fileInput = new ByteArrayInputStream(bos.toByteArray());
            // 转换 MultipartFile
            String fieldName = multiFile.getName();
            String fileName = multiFile.getOriginalFilename();
            String contentType = multiFile.getContentType();
            multipartFile = getMulFileByFile(fileInput, fieldName, contentType, fileName);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                if (bos != null) {
                    bos.close();
                }
                if (fileInput != null) {
                    fileInput.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return multipartFile;
    }
}